<?php
/**
 * Copyright (C), 2021-2021, Shall Buy Life info. Co., Ltd.
 * FileName: BaseRequest.php
 * Description: 基础验证类
 *
 * @author lwl
 * @Create Date    2021/6/8 17:39
 * @Update Date    2021/6/8 17:39 By lwl
 * @version v1.0
 */
namespace Example\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Response;
use ShallBuy\LaravelBase\Common\Exception\ExceptionFactoryFacade;

class BaseRequest extends FormRequest
{
    public $scenes = [];
    public $currentScene;               //当前场景
    public $autoValidate = false;       //是否注入之后自动验证

    /**
     * FunctionName：authorize
     * Description：是否鉴权
     * Author：lwl
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * FunctionName：scene
     * Description：设置场景
     * Author：lwl
     * @param $scene
     * @return $this
     */
    public function scene($scene)
    {
        $this->currentScene = $scene;
        return $this;
    }


    /**
     * FunctionName：validateResolved
     * Description：覆盖自动验证方法
     * Author：lwl
     */
    public function validateResolved()
    {
        if ($this->autoValidate) {
            $this->handleValidate();
        }
    }

    /**
     * FunctionName：validate
     * Description：验证方法
     * Author：lwl
     * @param string $scene
     * @return array|void
     */
    public function validate($scene = '')
    {
        if ($scene) {
            $this->currentScene = $scene;
        }
        $this->handleValidate();
    }

    /**
     * FunctionName：getRules
     * Description：根据场景获取规则
     * Author：lwl
     * @return array|mixed
     */
    public function getRules()
    {
        $rules = $this->container->call([$this, 'rules']);
        $newRules = [];
        if ($this->currentScene && isset($this->scenes[$this->currentScene])) {
            $sceneFields = is_array($this->scenes[$this->currentScene])
                ? $this->scenes[$this->currentScene] : explode(',', $this->scenes[$this->currentScene]);
            foreach ($sceneFields as $field) {
                if (array_key_exists($field, $rules)) {
                    $newRules[$field] = $rules[$field];
                }
            }
            return $newRules;
        }
        return $rules;
    }

    /**
     * FunctionName：validator
     * Description：自定义验证器
     * Author：lwl
     * @param $factory
     * @return mixed
     */
    public function validator($factory)
    {
        return $factory->make(
            $this->validationData(), $this->getRules(),
            $this->messages(), $this->attributes()
        );
    }

    /**
     * FunctionName：handleValidate
     * Description：最终验证方法
     * Author：lwl
     */
    protected function handleValidate()
    {
        $instance = $this->getValidatorInstance();
        if ($instance->fails()) {
            $this->failedValidation($instance);
        }
        $this->passedValidation();
    }

    /**
     * FunctionName：failedValidation
     * Description：重写报错部分-适应API JSON下发的需求
     * Author：lwl
     * @param Validator $validator
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    protected function failedValidation(Validator $validator)
    {
        throw ExceptionFactoryFacade::business( ['code' => Response::HTTP_BAD_REQUEST,'message' => $validator->errors()->first()]);
    }

}
